package com.swings.application;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.HashMap;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import com.swings.application.utility.CaseSignalProcessor;
import com.swings.application.utility.casesignal.CaseDetail;
import com.swings.application.utility.casesignal.CaseDetailList;
import com.swings.base.UtilityBasePanel;

/**
 * <p>
 * <b>Overview:</b>
 * <p>
 * 
 * 
 * <pre>
 * Creation date: Feb 16, 2015
 * @author Amit Kshirsagar
 * @version 1.0
 * @since
 * 
 * <p><b>Modification History:</b><p>
 * 
 * 
 * </pre>
 */

public class CaseSignalUtilityPanel extends UtilityBasePanel {
	static Logger log = Logger.getLogger(CaseSignalUtilityPanel.class.getName());

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.swings.base.UtilityBasePanel#populateCenterPanel()
	 */
	@Override
	protected void populateCenterPanel() {
		super.populateCenterPanel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.swings.base.UtilityBasePanelActionHandler#actionPerformed(java.awt
	 * .event.ActionEvent)
	 */
	@Override
	public void actionPerformed(final ActionEvent event) {
		super.actionPerformed(event);
		final String command = event.getActionCommand();
		Runnable actionRunner = new Runnable() {

			@Override
			public void run() {
				if (command.equalsIgnoreCase("GETCASECOUNT")) {
					executeGetCaseCount();
				} else if (command.equalsIgnoreCase("STARTCASEPROCESS") || command.equalsIgnoreCase("PAUSECASEPROCESS")
						|| command.equalsIgnoreCase("STOPCASEPROCESS")) {
					try {
						executeStartPauseStopCaseProcess(command);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					executeNoActionMessage(event);
				}
			}
		};

		Thread actionThread = new Thread(actionRunner, event.getSource().toString());
		actionThread.start();
	}

	private HashMap<String, CaseSignalProcessor> caseSignalProcessorHashMap = new HashMap<String, CaseSignalProcessor>();
	private CaseSignalProcessor caseSignalProcessor = null;
	private Thread caseSignalThread = null;

	/**
	 * @param command
	 * @throws InterruptedException
	 */
	protected void executeStartPauseStopCaseProcess(String command) throws InterruptedException {
		JComboBox phraseInProcess = (JComboBox) getFormComponent("PhraseInProcess");
		if (command.equalsIgnoreCase("STARTCASEPROCESS")) {
			getButton("STARTCASEPROCESS").setEnabled(false);
			getButton("PAUSECASEPROCESS").setEnabled(true);
			getButton("STOPCASEPROCESS").setEnabled(true);

			caseSignalProcessor = caseSignalProcessorHashMap.get(phraseInProcess.getSelectedItem());
			phraseInProcess.setEnabled(false);

			if (!caseSignalProcessor.isStarted()) {
				caseSignalThread = new Thread(caseSignalProcessor);
				caseSignalThread.run();
			} else {
				caseSignalProcessor.resumeThread();
				synchronized (caseSignalProcessor) {
					caseSignalProcessor.notify();
				}
			}
			log.debug("Execution Started...");
		} else if (command.equalsIgnoreCase("PAUSECASEPROCESS")) {
			getButton("STARTCASEPROCESS").setEnabled(true);
			getButton("PAUSECASEPROCESS").setEnabled(false);
			getButton("STOPCASEPROCESS").setEnabled(true);
			phraseInProcess.setEnabled(true);
			caseSignalProcessor.pauseThread();
			log.debug("Execution Paused...");
		} else if (command.equalsIgnoreCase("STOPCASEPROCESS")) {
			getButton("STARTCASEPROCESS").setEnabled(true);
			getButton("PAUSECASEPROCESS").setEnabled(false);
			getButton("STOPCASEPROCESS").setEnabled(false);
			caseSignalProcessor.pauseThread();
			log.debug("Execution Stopped...");
			phraseInProcess.setEnabled(true);
			caseSignalProcessor = null;
		}
	}

	public void executeGetCaseCount() {
		log.debug("Getting Case Count....");
		for (int i = 0; i < 11; i++) {
			getMainFrame().setProgressStatus(i * 10, "Case Count Collected: " + i * 10 + "%");
			sleep(100);
		}
		JTextField caseCount = (JTextField) getFormComponent("CaseCount");
		if (!caseSignalProcessorHashMap.containsKey(((JTextField) getFormComponent("PhraseName")).getText())) {
			caseCount.setText("" + Math.round(Math.random() * 20) * 100);
			caseCount.setBackground(Color.RED);

			CaseDetailList caseDetailList = new CaseDetailList();
			CaseDetail caseDetail = null;
			for (int i = 0; i < Integer.parseInt(caseCount.getText()); i++) {
				caseDetail = new CaseDetail();
				caseDetail.setCaseNumber(caseCount.getText() + "");
				caseDetailList.addCaseDetail(caseDetail);
			}

			caseSignalProcessor = new CaseSignalProcessor(((JTextField) getFormComponent("PhraseName")).getText(),
					caseDetailList.size() + "", mainFrame);

			caseSignalProcessorHashMap.put(caseSignalProcessor.getPhraseName(), caseSignalProcessor);
			JComboBox phraseInProcess = (JComboBox) getFormComponent("PhraseInProcess");
			phraseInProcess.addItem(caseSignalProcessor.getPhraseName());
			phraseInProcess.setSelectedItem(caseSignalProcessor.getPhraseName());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.swings.base.UtilityBasePanelActionHandler#itemStateChanged(java.awt
	 * .event.ItemEvent)
	 */
	@Override
	public void itemStateChanged(ItemEvent event) {
		String phraseFileName = (String) event.getItem();
		if (event.getStateChange() == ItemEvent.SELECTED) {

			caseSignalProcessorHashMap.get(phraseFileName).updateProgressStatus();

			((JTextField) getFormComponent("PhraseName")).setText(phraseFileName);
			((JTextField) getFormComponent("CaseCount")).setText(caseSignalProcessorHashMap.get(phraseFileName)
					.getCaseDetailList()
					+ "");

			log.debug(phraseFileName);
		}
		if (caseSignalProcessorHashMap.get(phraseFileName).isStarted()) {
			if (caseSignalProcessorHashMap.get(phraseFileName).isRunning()) {
				getButton("STARTCASEPROCESS").setEnabled(false);
				getButton("PAUSECASEPROCESS").setEnabled(true);
				getButton("STOPCASEPROCESS").setEnabled(true);
			} else {
				getButton("STARTCASEPROCESS").setEnabled(true);
				getButton("PAUSECASEPROCESS").setEnabled(false);
				getButton("STOPCASEPROCESS").setEnabled(true);
			}
		} else {
			getButton("STARTCASEPROCESS").setEnabled(true);
			getButton("PAUSECASEPROCESS").setEnabled(false);
			getButton("STOPCASEPROCESS").setEnabled(false);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.swings.base.UtilityBasePanelActionHandler#executeNoActionMessage(
	 * java.awt.event.ActionEvent)
	 */
	@Override
	protected void executeNoActionMessage(ActionEvent event) {
		log.debug("NoActionEvent disabled in Panel: " + getName());
	}

	private void sleep(int sleepTimemlSec) {
		try {
			Thread.sleep(sleepTimemlSec);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
